<template>
  <div>
    <el-card class="box-card">
      <h2>删除客户</h2>
      <el-form :model="customerForm" label-width="120px">
        <el-form-item label="选择客户：">
          <el-select v-model="selectedCustomer" placeholder="请选择客户">
            <el-option
              v-for="customer in customers"
              :key="customer.id"
              :label="customer.name"
              :value="customer.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="deleteCustomer">删除</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      customerForm: {
        name: '',
        phone: '',
        address: '',
        fax: '',
        email: '',
        bankName: '',
        bankAccount: '',
        remarks: ''
      },
      customers: [],
      selectedCustomer: null
    };
  },
  created() {
    this.fetchCustomers();
  },
  methods: {
    fetchCustomers() {
      this.axios.get('/api/customer/get')
        .then(response => {
          const result = response.data;
          if (Array.isArray(result.data)) {
            this.customers = result.data;
          } else {
            console.error('Error: Expected an array but got:', result.data);
          }
        })
        .catch(error => {
          console.error('Error fetching customers:', error);
        });
    },
    deleteCustomer() {
      // 获取选中客户的信息，这里可以根据需要获取客户的其他信息
      const selectedCustomerName = this.customers.find(customer => customer.id === this.selectedCustomer).name;

      // 发送删除请求，假设删除接口为 '/api/customers/delete'
      console.log('要删除的客户名称:', selectedCustomerName); // 打印选中客户的名称
      this.axios.delete(`/api/customer/delete/${selectedCustomerName}`)
        .then(() => {
          this.$message({
            message: '客户删除成功',
            type: 'success'
          });
          // 删除成功后执行其他操作，例如刷新客户列表
          this.fetchCustomers();
        })
        .catch(error => {
          console.error('Error deleting customer:', error);
          this.$message.error('客户删除失败');
        });
    }
  }
}
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}
</style>
